<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>filteredmenu.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_filteredmenu.js.html">filteredmenu.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Menu where items can be filtered based on user keyboard input.
<a name="line17"></a> * If a filter is specified only the items matching it will be displayed.
<a name="line18"></a> *
<a name="line19"></a> * @author eae@google.com (Emil A Eklund)
<a name="line20"></a> * @see ../demos/filteredmenu.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.ui.FilteredMenu&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.InputHandler&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line30"></a>goog.require(&#39;goog.string&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.FilterObservingMenuItem&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.Menu&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Filtered menu class.
<a name="line38"></a> * @param {goog.ui.MenuRenderer=} opt_renderer Renderer used to render filtered
<a name="line39"></a> *     menu; defaults to {@link goog.ui.MenuRenderer}.
<a name="line40"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
<a name="line41"></a> * @constructor
<a name="line42"></a> * @extends {goog.ui.Menu}
<a name="line43"></a> */
<a name="line44"></a>goog.ui.FilteredMenu = function(opt_renderer, opt_domHelper) {
<a name="line45"></a>  goog.ui.Menu.call(this, opt_domHelper, opt_renderer);
<a name="line46"></a>};
<a name="line47"></a>goog.inherits(goog.ui.FilteredMenu, goog.ui.Menu);
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * Events fired by component.
<a name="line52"></a> * @enum {string}
<a name="line53"></a> */
<a name="line54"></a>goog.ui.FilteredMenu.EventType = {
<a name="line55"></a>  /** Dispatched after the component filter criteria has been changed. */
<a name="line56"></a>  FILTER_CHANGED: &#39;filterchange&#39;
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * Filter input element.
<a name="line62"></a> * @type {Element|undefined}
<a name="line63"></a> * @private
<a name="line64"></a> */
<a name="line65"></a>goog.ui.FilteredMenu.prototype.filterInput_;
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * The input handler that provides the input event.
<a name="line70"></a> * @type {goog.events.InputHandler|undefined}
<a name="line71"></a> * @private
<a name="line72"></a> */
<a name="line73"></a>goog.ui.FilteredMenu.prototype.inputHandler_;
<a name="line74"></a>
<a name="line75"></a>
<a name="line76"></a>/**
<a name="line77"></a> * Maximum number of characters for filter input.
<a name="line78"></a> * @type {number}
<a name="line79"></a> * @private
<a name="line80"></a> */
<a name="line81"></a>goog.ui.FilteredMenu.prototype.maxLength_ = 0;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Label displayed in the filter input when no text has been entered.
<a name="line86"></a> * @type {string}
<a name="line87"></a> * @private
<a name="line88"></a> */
<a name="line89"></a>goog.ui.FilteredMenu.prototype.label_ = &#39;&#39;;
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>/**
<a name="line93"></a> * Label element.
<a name="line94"></a> * @type {Element|undefined}
<a name="line95"></a> * @private
<a name="line96"></a> */
<a name="line97"></a>goog.ui.FilteredMenu.prototype.labelEl_;
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Whether multiple items can be entered comma separated.
<a name="line102"></a> * @type {boolean}
<a name="line103"></a> * @private
<a name="line104"></a> */
<a name="line105"></a>goog.ui.FilteredMenu.prototype.allowMultiple_ = false;
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * List of items entered in the search box if multiple entries are allowed.
<a name="line110"></a> * @type {Array.&lt;string&gt;|undefined}
<a name="line111"></a> * @private
<a name="line112"></a> */
<a name="line113"></a>goog.ui.FilteredMenu.prototype.enteredItems_;
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/**
<a name="line117"></a> * Index of first item that should be affected by the filter. Menu items with
<a name="line118"></a> * a lower index will not be affected by the filter.
<a name="line119"></a> * @type {number}
<a name="line120"></a> * @private
<a name="line121"></a> */
<a name="line122"></a>goog.ui.FilteredMenu.prototype.filterFromIndex_ = 0;
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Filter applied to the menu.
<a name="line127"></a> * @type {string|undefined|null}
<a name="line128"></a> * @private
<a name="line129"></a> */
<a name="line130"></a>goog.ui.FilteredMenu.prototype.filterStr_;
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Map of child nodes that shouldn&#39;t be affected by filtering.
<a name="line135"></a> * @type {Object|undefined}
<a name="line136"></a> * @private
<a name="line137"></a> */
<a name="line138"></a>goog.ui.FilteredMenu.prototype.persistentChildren_;
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/** @override */
<a name="line142"></a>goog.ui.FilteredMenu.prototype.createDom = function() {
<a name="line143"></a>  goog.ui.FilteredMenu.superClass_.createDom.call(this);
<a name="line144"></a>
<a name="line145"></a>  var dom = this.getDomHelper();
<a name="line146"></a>  var el = dom.createDom(&#39;div&#39;,
<a name="line147"></a>      goog.getCssName(this.getRenderer().getCssClass(), &#39;filter&#39;),
<a name="line148"></a>      this.labelEl_ = dom.createDom(&#39;div&#39;, null, this.label_),
<a name="line149"></a>      this.filterInput_ = dom.createDom(&#39;input&#39;, {&#39;type&#39;: &#39;text&#39;}));
<a name="line150"></a>  var element = this.getElement();
<a name="line151"></a>  dom.appendChild(element, el);
<a name="line152"></a>  this.contentElement_ = dom.createDom(&#39;div&#39;,
<a name="line153"></a>      goog.getCssName(this.getRenderer().getCssClass(), &#39;content&#39;));
<a name="line154"></a>  dom.appendChild(element, this.contentElement_);
<a name="line155"></a>
<a name="line156"></a>  this.initFilterInput_();
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Helper method that initializes the filter input element.
<a name="line162"></a> * @private
<a name="line163"></a> */
<a name="line164"></a>goog.ui.FilteredMenu.prototype.initFilterInput_ = function() {
<a name="line165"></a>  this.setFocusable(true);
<a name="line166"></a>  this.setKeyEventTarget(this.filterInput_);
<a name="line167"></a>
<a name="line168"></a>  // Workaround for mozilla bug #236791.
<a name="line169"></a>  if (goog.userAgent.GECKO) {
<a name="line170"></a>    this.filterInput_.setAttribute(&#39;autocomplete&#39;, &#39;off&#39;);
<a name="line171"></a>  }
<a name="line172"></a>
<a name="line173"></a>  if (this.maxLength_) {
<a name="line174"></a>    this.filterInput_.maxLength = this.maxLength_;
<a name="line175"></a>  }
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Sets up listeners and prepares the filter functionality.
<a name="line181"></a> * @private
<a name="line182"></a> */
<a name="line183"></a>goog.ui.FilteredMenu.prototype.setUpFilterListeners_ = function() {
<a name="line184"></a>  if (!this.inputHandler_ &amp;&amp; this.filterInput_) {
<a name="line185"></a>    this.inputHandler_ = new goog.events.InputHandler(
<a name="line186"></a>        /** @type {Element} */ (this.filterInput_));
<a name="line187"></a>    goog.style.setUnselectable(this.filterInput_, false);
<a name="line188"></a>    goog.events.listen(this.inputHandler_,
<a name="line189"></a>                       goog.events.InputHandler.EventType.INPUT,
<a name="line190"></a>                       this.handleFilterEvent, false, this);
<a name="line191"></a>    goog.events.listen(this.filterInput_.parentNode,
<a name="line192"></a>                       goog.events.EventType.CLICK,
<a name="line193"></a>                       this.onFilterLabelClick_, false, this);
<a name="line194"></a>    if (this.allowMultiple_) {
<a name="line195"></a>      this.enteredItems_ = [];
<a name="line196"></a>    }
<a name="line197"></a>  }
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Tears down listeners and resets the filter functionality.
<a name="line203"></a> * @private
<a name="line204"></a> */
<a name="line205"></a>goog.ui.FilteredMenu.prototype.tearDownFilterListeners_ = function() {
<a name="line206"></a>  if (this.inputHandler_) {
<a name="line207"></a>    goog.events.unlisten(this.inputHandler_,
<a name="line208"></a>                         goog.events.InputHandler.EventType.INPUT,
<a name="line209"></a>                         this.handleFilterEvent, false, this);
<a name="line210"></a>    goog.events.unlisten(this.filterInput_.parentNode,
<a name="line211"></a>                         goog.events.EventType.CLICK,
<a name="line212"></a>                         this.onFilterLabelClick_, false, this);
<a name="line213"></a>
<a name="line214"></a>    this.inputHandler_.dispose();
<a name="line215"></a>    this.inputHandler_ = undefined;
<a name="line216"></a>    this.enteredItems_ = undefined;
<a name="line217"></a>  }
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/** @override */
<a name="line222"></a>goog.ui.FilteredMenu.prototype.setVisible = function(show, opt_force, opt_e) {
<a name="line223"></a>  var visibilityChanged = goog.ui.FilteredMenu.superClass_.setVisible.call(this,
<a name="line224"></a>      show, opt_force, opt_e);
<a name="line225"></a>  if (visibilityChanged &amp;&amp; show &amp;&amp; this.isInDocument()) {
<a name="line226"></a>    this.setFilter(&#39;&#39;);
<a name="line227"></a>    this.setUpFilterListeners_();
<a name="line228"></a>  } else if (visibilityChanged &amp;&amp; !show) {
<a name="line229"></a>    this.tearDownFilterListeners_();
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  return visibilityChanged;
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/** @override */
<a name="line237"></a>goog.ui.FilteredMenu.prototype.disposeInternal = function() {
<a name="line238"></a>  this.tearDownFilterListeners_();
<a name="line239"></a>  this.filterInput_ = undefined;
<a name="line240"></a>  this.labelEl_ = undefined;
<a name="line241"></a>  goog.ui.FilteredMenu.superClass_.disposeInternal.call(this);
<a name="line242"></a>};
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>/**
<a name="line246"></a> * Sets the filter label (the label displayed in the filter input element if no
<a name="line247"></a> * text has been entered).
<a name="line248"></a> * @param {?string} label Label text.
<a name="line249"></a> */
<a name="line250"></a>goog.ui.FilteredMenu.prototype.setFilterLabel = function(label) {
<a name="line251"></a>  this.label_ = label || &#39;&#39;;
<a name="line252"></a>  if (this.labelEl_) {
<a name="line253"></a>    goog.dom.setTextContent(this.labelEl_, this.label_);
<a name="line254"></a>  }
<a name="line255"></a>};
<a name="line256"></a>
<a name="line257"></a>
<a name="line258"></a>/**
<a name="line259"></a> * @return {string} The filter label.
<a name="line260"></a> */
<a name="line261"></a>goog.ui.FilteredMenu.prototype.getFilterLabel = function() {
<a name="line262"></a>  return this.label_;
<a name="line263"></a>};
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * Sets the filter string.
<a name="line268"></a> * @param {?string} str Filter string.
<a name="line269"></a> */
<a name="line270"></a>goog.ui.FilteredMenu.prototype.setFilter = function(str) {
<a name="line271"></a>  if (this.filterInput_) {
<a name="line272"></a>    this.filterInput_.value = str;
<a name="line273"></a>    this.filterItems_(str);
<a name="line274"></a>  }
<a name="line275"></a>};
<a name="line276"></a>
<a name="line277"></a>
<a name="line278"></a>/**
<a name="line279"></a> * Returns the filter string.
<a name="line280"></a> * @return {string} Current filter or an an empty string.
<a name="line281"></a> */
<a name="line282"></a>goog.ui.FilteredMenu.prototype.getFilter = function() {
<a name="line283"></a>  return this.filterInput_ &amp;&amp; goog.isString(this.filterInput_.value) ?
<a name="line284"></a>      this.filterInput_.value : &#39;&#39;;
<a name="line285"></a>};
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>/**
<a name="line289"></a> * Sets the index of first item that should be affected by the filter. Menu
<a name="line290"></a> * items with a lower index will not be affected by the filter.
<a name="line291"></a> * @param {number} index Index of first item that should be affected by filter.
<a name="line292"></a> */
<a name="line293"></a>goog.ui.FilteredMenu.prototype.setFilterFromIndex = function(index) {
<a name="line294"></a>  this.filterFromIndex_ = index;
<a name="line295"></a>};
<a name="line296"></a>
<a name="line297"></a>
<a name="line298"></a>/**
<a name="line299"></a> * Returns the index of first item that is affected by the filter.
<a name="line300"></a> * @return {number} Index of first item that is affected by filter.
<a name="line301"></a> */
<a name="line302"></a>goog.ui.FilteredMenu.prototype.getFilterFromIndex = function() {
<a name="line303"></a>  return this.filterFromIndex_;
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>/**
<a name="line308"></a> * Gets a list of items entered in the search box.
<a name="line309"></a> * @return {Array.&lt;string&gt;} The entered items.
<a name="line310"></a> */
<a name="line311"></a>goog.ui.FilteredMenu.prototype.getEnteredItems = function() {
<a name="line312"></a>  return this.enteredItems_ || [];
<a name="line313"></a>};
<a name="line314"></a>
<a name="line315"></a>
<a name="line316"></a>/**
<a name="line317"></a> * Sets whether multiple items can be entered comma separated.
<a name="line318"></a> * @param {boolean} b Whether multiple items can be entered.
<a name="line319"></a> */
<a name="line320"></a>goog.ui.FilteredMenu.prototype.setAllowMultiple = function(b) {
<a name="line321"></a>  this.allowMultiple_ = b;
<a name="line322"></a>};
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/**
<a name="line326"></a> * @return {boolean} Whether multiple items can be entered comma separated.
<a name="line327"></a> */
<a name="line328"></a>goog.ui.FilteredMenu.prototype.getAllowMultiple = function() {
<a name="line329"></a>  return this.allowMultiple_;
<a name="line330"></a>};
<a name="line331"></a>
<a name="line332"></a>
<a name="line333"></a>/**
<a name="line334"></a> * Sets whether the specified child should be affected (shown/hidden) by the
<a name="line335"></a> * filter criteria.
<a name="line336"></a> * @param {goog.ui.Component} child Child to change.
<a name="line337"></a> * @param {boolean} persistent Whether the child should be persistent.
<a name="line338"></a> */
<a name="line339"></a>goog.ui.FilteredMenu.prototype.setPersistentVisibility = function(child,
<a name="line340"></a>                                                                  persistent) {
<a name="line341"></a>  if (!this.persistentChildren_) {
<a name="line342"></a>    this.persistentChildren_ = {};
<a name="line343"></a>  }
<a name="line344"></a>  this.persistentChildren_[child.getId()] = persistent;
<a name="line345"></a>};
<a name="line346"></a>
<a name="line347"></a>
<a name="line348"></a>/**
<a name="line349"></a> * Returns whether the specified child should be affected (shown/hidden) by the
<a name="line350"></a> * filter criteria.
<a name="line351"></a> * @param {goog.ui.Component} child Menu item to check.
<a name="line352"></a> * @return {boolean} Whether the menu item is persistent.
<a name="line353"></a> */
<a name="line354"></a>goog.ui.FilteredMenu.prototype.hasPersistentVisibility = function(child) {
<a name="line355"></a>  return !!(this.persistentChildren_ &amp;&amp;
<a name="line356"></a>            this.persistentChildren_[child.getId()]);
<a name="line357"></a>};
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * Handles filter input events.
<a name="line362"></a> * @param {goog.events.BrowserEvent} e The event object.
<a name="line363"></a> */
<a name="line364"></a>goog.ui.FilteredMenu.prototype.handleFilterEvent = function(e) {
<a name="line365"></a>  this.filterItems_(this.filterInput_.value);
<a name="line366"></a>
<a name="line367"></a>  // Highlight the first visible item unless there&#39;s already a highlighted item.
<a name="line368"></a>  var highlighted = this.getHighlighted();
<a name="line369"></a>  if (!highlighted || !highlighted.isVisible()) {
<a name="line370"></a>    this.highlightFirst();
<a name="line371"></a>  }
<a name="line372"></a>  this.dispatchEvent(goog.ui.FilteredMenu.EventType.FILTER_CHANGED);
<a name="line373"></a>};
<a name="line374"></a>
<a name="line375"></a>
<a name="line376"></a>/**
<a name="line377"></a> * Shows/hides elements based on the supplied filter.
<a name="line378"></a> * @param {?string} str Filter string.
<a name="line379"></a> * @private
<a name="line380"></a> */
<a name="line381"></a>goog.ui.FilteredMenu.prototype.filterItems_ = function(str) {
<a name="line382"></a>  // Do nothing unless the filter string has changed.
<a name="line383"></a>  if (this.filterStr_ == str) {
<a name="line384"></a>    return;
<a name="line385"></a>  }
<a name="line386"></a>
<a name="line387"></a>  if (this.labelEl_) {
<a name="line388"></a>    this.labelEl_.style.visibility = str == &#39;&#39; ? &#39;visible&#39; : &#39;hidden&#39;;
<a name="line389"></a>  }
<a name="line390"></a>
<a name="line391"></a>  if (this.allowMultiple_ &amp;&amp; this.enteredItems_) {
<a name="line392"></a>    // Matches all non space characters after the last comma.
<a name="line393"></a>    var lastWordRegExp = /^(.+),[ ]*([^,]*)$/;
<a name="line394"></a>    var matches = str.match(lastWordRegExp);
<a name="line395"></a>    // matches[1] is the string up to, but not including, the last comma and
<a name="line396"></a>    // matches[2] the part after the last comma. If there are no non-space
<a name="line397"></a>    // characters after the last comma matches[2] is undefined.
<a name="line398"></a>    var items = matches &amp;&amp; matches[1] ? matches[1].split(&#39;,&#39;) : [];
<a name="line399"></a>
<a name="line400"></a>    // If the number of comma separated items has changes recreate the
<a name="line401"></a>    // entered items array and fire a change event.
<a name="line402"></a>    if (str.substr(str.length - 1, 1) == &#39;,&#39; ||
<a name="line403"></a>        items.length != this.enteredItems_.length) {
<a name="line404"></a>      var lastItem = items[items.length - 1] || &#39;&#39;;
<a name="line405"></a>
<a name="line406"></a>      // Auto complete text in input box based on the highlighted item.
<a name="line407"></a>      if (this.getHighlighted() &amp;&amp; lastItem != &#39;&#39;) {
<a name="line408"></a>        var caption = this.getHighlighted().getCaption();
<a name="line409"></a>        if (caption.toLowerCase().indexOf(lastItem.toLowerCase()) == 0) {
<a name="line410"></a>          items[items.length - 1] = caption;
<a name="line411"></a>          this.filterInput_.value = items.join(&#39;,&#39;) + &#39;,&#39;;
<a name="line412"></a>        }
<a name="line413"></a>      }
<a name="line414"></a>      this.enteredItems_ = items;
<a name="line415"></a>      this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
<a name="line416"></a>      this.setHighlightedIndex(-1);
<a name="line417"></a>    }
<a name="line418"></a>
<a name="line419"></a>    if (matches) {
<a name="line420"></a>      str = matches.length &gt; 2 ? goog.string.trim(matches[2]) : &#39;&#39;;
<a name="line421"></a>    }
<a name="line422"></a>  }
<a name="line423"></a>
<a name="line424"></a>  var matcher = new RegExp(&#39;(^|[- ,_/.:])&#39; +
<a name="line425"></a>      goog.string.regExpEscape(str), &#39;i&#39;);
<a name="line426"></a>  for (var child, i = this.filterFromIndex_; child = this.getChildAt(i); i++) {
<a name="line427"></a>    if (child instanceof goog.ui.FilterObservingMenuItem) {
<a name="line428"></a>      child.callObserver(str);
<a name="line429"></a>    } else if (!this.hasPersistentVisibility(child)) {
<a name="line430"></a>      // Only show items matching the filter and highlight the part of the
<a name="line431"></a>      // caption that matches.
<a name="line432"></a>      var caption = child.getCaption();
<a name="line433"></a>      if (caption) {
<a name="line434"></a>        var matchArray = caption.match(matcher);
<a name="line435"></a>        if (str == &#39;&#39; || matchArray) {
<a name="line436"></a>          child.setVisible(true);
<a name="line437"></a>          var pos = caption.indexOf(matchArray[0]);
<a name="line438"></a>
<a name="line439"></a>          // If position is non zero increase by one to skip the separator.
<a name="line440"></a>          if (pos) {
<a name="line441"></a>            pos++;
<a name="line442"></a>          }
<a name="line443"></a>
<a name="line444"></a>          if (str == &#39;&#39;) {
<a name="line445"></a>            child.setContent(caption);
<a name="line446"></a>          } else {
<a name="line447"></a>            child.setContent(this.getDomHelper().createDom(&#39;span&#39;, null,
<a name="line448"></a>                caption.substr(0, pos),
<a name="line449"></a>                this.getDomHelper().createDom(
<a name="line450"></a>                    &#39;b&#39;, null, caption.substr(pos, str.length)),
<a name="line451"></a>                caption.substr(pos + str.length,
<a name="line452"></a>                    caption.length - str.length - pos)));
<a name="line453"></a>          }
<a name="line454"></a>        } else {
<a name="line455"></a>          child.setVisible(false);
<a name="line456"></a>        }
<a name="line457"></a>      } else {
<a name="line458"></a>
<a name="line459"></a>        // Hide separators and other items without a caption if a filter string
<a name="line460"></a>        // has been entered.
<a name="line461"></a>        child.setVisible(str == &#39;&#39;);
<a name="line462"></a>      }
<a name="line463"></a>    }
<a name="line464"></a>  }
<a name="line465"></a>  this.filterStr_ = str;
<a name="line466"></a>};
<a name="line467"></a>
<a name="line468"></a>
<a name="line469"></a>/**
<a name="line470"></a> * Handles the menu&#39;s behavior for a key event. The highlighted menu item will
<a name="line471"></a> * be given the opportunity to handle the key behavior.
<a name="line472"></a> * @param {goog.events.KeyEvent} e A browser event.
<a name="line473"></a> * @return {boolean} Whether the event was handled.
<a name="line474"></a> * @override
<a name="line475"></a> */
<a name="line476"></a>goog.ui.FilteredMenu.prototype.handleKeyEventInternal = function(e) {
<a name="line477"></a>  // Home, end and the arrow keys are normally used to change the selected menu
<a name="line478"></a>  // item. Return false here to prevent the menu from preventing the default
<a name="line479"></a>  // behavior for HOME, END and any key press with a modifier.
<a name="line480"></a>  if (e.shiftKey || e.ctrlKey || e.altKey ||
<a name="line481"></a>      e.keyCode == goog.events.KeyCodes.HOME ||
<a name="line482"></a>      e.keyCode == goog.events.KeyCodes.END) {
<a name="line483"></a>    return false;
<a name="line484"></a>  }
<a name="line485"></a>
<a name="line486"></a>  if (e.keyCode == goog.events.KeyCodes.ESC) {
<a name="line487"></a>    this.dispatchEvent(goog.ui.Component.EventType.BLUR);
<a name="line488"></a>    return true;
<a name="line489"></a>  }
<a name="line490"></a>
<a name="line491"></a>  return goog.ui.FilteredMenu.superClass_.handleKeyEventInternal.call(this, e);
<a name="line492"></a>};
<a name="line493"></a>
<a name="line494"></a>
<a name="line495"></a>/**
<a name="line496"></a> * Sets the highlighted index, unless the HIGHLIGHT event is intercepted and
<a name="line497"></a> * cancelled.  -1 = no highlight. Also scrolls the menu item into view.
<a name="line498"></a> * @param {number} index Index of menu item to highlight.
<a name="line499"></a> * @override
<a name="line500"></a> */
<a name="line501"></a>goog.ui.FilteredMenu.prototype.setHighlightedIndex = function(index) {
<a name="line502"></a>  goog.ui.FilteredMenu.superClass_.setHighlightedIndex.call(this, index);
<a name="line503"></a>  var contentEl = this.getContentElement();
<a name="line504"></a>  var el = this.getHighlighted() ? this.getHighlighted().getElement() : null;
<a name="line505"></a>
<a name="line506"></a>  if (el &amp;&amp; goog.dom.contains(contentEl, el)) {
<a name="line507"></a>    var contentTop = goog.userAgent.IE &amp;&amp; !goog.userAgent.isVersion(8) ?
<a name="line508"></a>        0 : contentEl.offsetTop;
<a name="line509"></a>
<a name="line510"></a>    // IE (tested on IE8) sometime does not scroll enough by about
<a name="line511"></a>    // 1px. So we add 1px to the scroll amount. This still looks ok in
<a name="line512"></a>    // other browser except for the most degenerate case (menu height &lt;=
<a name="line513"></a>    // item height).
<a name="line514"></a>
<a name="line515"></a>    // Scroll down if the highlighted item is below the bottom edge.
<a name="line516"></a>    var diff = (el.offsetTop + el.offsetHeight - contentTop) -
<a name="line517"></a>        (contentEl.clientHeight + contentEl.scrollTop) + 1;
<a name="line518"></a>    contentEl.scrollTop += Math.max(diff, 0);
<a name="line519"></a>
<a name="line520"></a>    // Scroll up if the highlighted item is above the top edge.
<a name="line521"></a>    diff = contentEl.scrollTop - (el.offsetTop - contentTop) + 1;
<a name="line522"></a>    contentEl.scrollTop -= Math.max(diff, 0);
<a name="line523"></a>  }
<a name="line524"></a>};
<a name="line525"></a>
<a name="line526"></a>
<a name="line527"></a>/**
<a name="line528"></a> * Handles clicks on the filter label. Focuses the input element.
<a name="line529"></a> * @param {goog.events.BrowserEvent} e A browser event.
<a name="line530"></a> * @private
<a name="line531"></a> */
<a name="line532"></a>goog.ui.FilteredMenu.prototype.onFilterLabelClick_ = function(e) {
<a name="line533"></a>  this.filterInput_.focus();
<a name="line534"></a>};
<a name="line535"></a>
<a name="line536"></a>
<a name="line537"></a>/** @override */
<a name="line538"></a>goog.ui.FilteredMenu.prototype.getContentElement = function() {
<a name="line539"></a>  return this.contentElement_ || this.getElement();
<a name="line540"></a>};
<a name="line541"></a>
<a name="line542"></a>
<a name="line543"></a>/**
<a name="line544"></a> * Returns the filter input element.
<a name="line545"></a> * @return {Element} Input element.
<a name="line546"></a> */
<a name="line547"></a>goog.ui.FilteredMenu.prototype.getFilterInputElement = function() {
<a name="line548"></a>  return this.filterInput_ || null;
<a name="line549"></a>};
<a name="line550"></a>
<a name="line551"></a>
<a name="line552"></a>/** @override */
<a name="line553"></a>goog.ui.FilteredMenu.prototype.decorateInternal = function(element) {
<a name="line554"></a>  this.setElementInternal(element);
<a name="line555"></a>
<a name="line556"></a>  // Decorate the menu content.
<a name="line557"></a>  this.decorateContent(element);
<a name="line558"></a>
<a name="line559"></a>  // Locate internally managed elements.
<a name="line560"></a>  var el = this.getDomHelper().getElementsByTagNameAndClass(&#39;div&#39;,
<a name="line561"></a>      goog.getCssName(this.getRenderer().getCssClass(), &#39;filter&#39;), element)[0];
<a name="line562"></a>  this.labelEl_ = goog.dom.getFirstElementChild(el);
<a name="line563"></a>  this.filterInput_ = goog.dom.getNextElementSibling(this.labelEl_);
<a name="line564"></a>  this.contentElement_ = goog.dom.getNextElementSibling(el);
<a name="line565"></a>
<a name="line566"></a>  // Decorate additional menu items (like &#39;apply&#39;).
<a name="line567"></a>  this.getRenderer().decorateChildren(this, el.parentNode,
<a name="line568"></a>      this.contentElement_);
<a name="line569"></a>
<a name="line570"></a>  this.initFilterInput_();
<a name="line571"></a>};
</pre>


</body>
</html>
